import Koa from 'koa';
import { logger } from '../models/LogModel';
import { IpList, port } from '../config/webcfg';
import bodyParser from 'koa-bodyparser';
import router from './src/router';
const app = new Koa();

app.use(async (ctx, next) => {
  // 安全组检测
  let status = false;
  IpList.map(ip => {
    if(ctx.ip.includes(ip) == true){
      status = true;
    }
  })

  if(status){
    logger('web').info(`${ctx.method} ${ctx.url}`);
  }else{
    logger('web').warn(`${ctx.method} ${ctx.url}`);
  }

  // 设置响应头
  ctx.set('Content-Type', 'application/json;charset: UTF-8');
  await next();
});

app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());

export default app;